package com.baizhi.common;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//全局异常类   需要实现HandlerExceptionResolver
public class GlobalException implements HandlerExceptionResolver {
    //如果controller发生异常就自动执行当前方法
    //第四个参数：存储了controller产生的异常
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse,
                                         Object o,
                                         Exception e) {
        ModelAndView mav = new ModelAndView();
        //将异常信息保存到request作用域内
        mav.addObject("msg",e.getMessage());
        mav.setViewName("forward:/error.jsp");
        return mav;
    }
}
